/*
 * Created by 岱青海蓝信息系统(北京)有限公司 on 17-4-24 下午2:05
 * Copyright (c) 2017. All rights reserved.
 */

package com.example.dqhl.recycleviewdemo.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import com.example.dqhl.recycleviewdemo.R;
import com.nineoldandroids.view.ViewHelper;

/**
 * Created by dqhl on 17/4/24.
 */
public class SlidingMenu extends HorizontalScrollView {


    private LinearLayout mWapper;
    private ViewGroup mMenu;
    private ViewGroup mContent;

    //获取屏幕宽度
    private int mScreenWidth;
    //ment 与屏幕右侧距离  DP
    private int mMenuPadding = 50;

    //判断onMeasure 是否多次调用

    private boolean once;

    private int mMenuWidth;

    private boolean isOpen;

    /**
     * 未使用自定义属性时,会调用两个参数的构造函数
     */
    public SlidingMenu(Context context, AttributeSet attrs) {
        this(context, attrs, 0);

    }

    public SlidingMenu(Context context) {
        this(context, null);
    }

    /**
     * 使用自定义属性时,会调用两个参数的构造函数
     */
    public SlidingMenu(Context context, AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);

        //获取定义的属性
        TypedArray array = context.getTheme().obtainStyledAttributes(attrs,
                R.styleable.SlidingMenu, defStyleAttr, 0);

        int number = array.getIndexCount();
        for (int i = 0; i < number; i++) {
            int attr = array.getIndex(i);
            switch (attr) {
                case R.styleable.SlidingMenu_rightPadding:

                    mMenuPadding = array.getDimensionPixelSize(attr,
                            (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources()
                                    .getDisplayMetrics()));
                    break;
            }
        }
        array.recycle();

        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrices = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrices);
        mScreenWidth = outMetrices.widthPixels;

        // 把dp转化为px
//        mMenuPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources()
//                .getDisplayMetrics());
    }

    /**
     * 自定义View  3步
     * <p/>
     * <p/>
     * 1.onMeasure  决定内部View(子View)的宽和高以及自己的宽和高
     * <p/>
     * 2.onLayout 决定子View的放置的位置
     * <p/>
     * 3.onTouchEvent 移动
     */


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        if (!once) {

            mWapper = (LinearLayout) getChildAt(0);
            mMenu = (ViewGroup) mWapper.getChildAt(0);
            mContent = (ViewGroup) mWapper.getChildAt(1);

            mMenuWidth = mMenu.getLayoutParams().width = mScreenWidth - mMenuPadding;
            mContent.getLayoutParams().width = mScreenWidth;

//            mWapper.getLayoutParams().width = mScreenWidth + m;
            once = true;
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /**
     * 通过设计偏移量 讲Menu隐藏
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (changed) {
            this.scrollTo(mMenuWidth, 0);
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        int action = ev.getAction();
        switch (action) {
            //判断当前
            case MotionEvent.ACTION_UP:
                //隐藏在左边的宽度
                int scrollX = getScrollX();
                if (scrollX >= mMenuWidth / 2) {
                    this.smoothScrollTo(mMenuWidth, 0);
                    //菜单隐藏
                    isOpen = false;

                } else {
                    this.smoothScrollTo(0, 0);
                    isOpen = true;
                }
                return true;
        }

        return super.onTouchEvent(ev);
    }



    //打开菜单
    public void openMenu() {
        if (isOpen)
            return;
        this.smoothScrollTo(0, 0);
        isOpen = true;
    }

    //关闭菜单
    public void closeMenu() {
        if (!isOpen)
            return;
        this.smoothScrollTo(mMenuWidth, 0);
        isOpen = false;
    }

    //切换菜单
    public void toggle() {
        if (isOpen) {
            closeMenu();
        } else {
            openMenu();
        }
    }

    /**
     * 滚动发生时
     */

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {

        super.onScrollChanged(l, t, oldl, oldt);

        float scale = l * 1.0f / mMenuWidth;

        float rightScale = 0.7f + 0.3f * scale;
        float leftScale = 1.0f - scale * 0.3f;
        float leftAlpha = 0.6f + 0.4f * (1 - scale);
        //调用属性动画,设置TraslationX
        ViewHelper.setTranslationX(mMenu, mMenuWidth * scale * 0.8f);
        ViewHelper.setScaleX(mMenu,leftScale);
        ViewHelper.setScaleY(mMenu,leftScale);
        ViewHelper.setAlpha(mMenu,leftAlpha);
        //设置缩放的中心点
        ViewHelper.setPivotX(mContent, 0);
        ViewHelper.setPivotY(mContent, mContent.getHeight() / 2);
        ViewHelper.setScaleX(mContent, rightScale);
        ViewHelper.setScaleY(mContent, rightScale);


    }
}
